package com.twang.stock.design.impl;

import com.alibaba.fastjson.JSONObject;
import com.twang.stock.design.IndexDataTask;
import com.twang.stock.dto.IndexedInfo;
import com.twang.stock.entity.TodayQinXu;
import com.twang.stock.entity.WenCaiData;
import com.twang.stock.redis.RedisUtil;
import com.twang.stock.task.NotifyStockBuyTask;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 大盘成交量
 */

@Service
@Slf4j
public class WenCaiDataTask extends IndexedInfo implements IndexDataTask {
    public static String cache_key = "WenCaiDataTask";
    public static String CACHE_KEY_TOP = "WenCaiDataTask_TOP_TEN";
    public static int size=10;


    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private NotifyStockBuyTask notifyStockBuyTask;


    @Override
    public void execute(ModelAndView modelAndView, CountDownLatch latch) {
        try {
            //
            Object cacheKeyZhangTingBanList = redisUtil.get(cache_key);
            if(null==cacheKeyZhangTingBanList){
                notifyStockBuyTask.年度涨幅前20();
                cacheKeyZhangTingBanList = redisUtil.get(cache_key);
            }
            Object todayQinXuObject = redisUtil.get(CACHE_KEY_TOP);
            if(null!=todayQinXuObject){
                TodayQinXu todayQinXu = JSONObject.parseObject(todayQinXuObject.toString(), TodayQinXu.class);
                modelAndView.addObject("nianDuZhangFuTop", todayQinXu);
            }
            List<WenCaiData> wenCaiDataList = JSONObject.parseArray(cacheKeyZhangTingBanList.toString(), WenCaiData.class);
            modelAndView.addObject("nianDuZhangFu", wenCaiDataList);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            log.info("大盘涨跌情况 执行完成，当前剩余条数:{}", latch.getCount());
            latch.countDown();
        }
    }


}
